<?php 
class Zen_Behavior_Email extends Zen_Behavior_Model {
	
	private $_form;
	
	public function beforeCreate($params) {
		$dparams= $params;
		$form = $this->getForm();
		$fields = $form->getElements ();
		$text = "";
		foreach ( $fields as $field ) {
			if ($field->getType () != "Zend_Form_Element_Submit") {
				$name = $field->getName ();
				$text .= "<b>";
				$text .= $field->getLabel () . " ";
				$text .= "</b>";
				if (isset ( $params [$name] )) {
					$data = "";
					if (is_array ( $params [$name] )) {
						foreach ( $params [$name] as $value ) {
							$data .= $value . "; ";
						}
					} else {
						$data = $params [$name];
					}
					$text .= $data;
				} else {
					$text .= "-------------------";
				}
				$text .= "<br/>";
			}
		}
		$text .= "<br/><br/>Это сообщение отправлено, роботом. Отвечать на него не нужно. Ваш ответ отправляйте, на адрес, указанный в поле email.";
		$params ['text'] = $text;
		$email = new Communication_Model_Email ();
		$globalConf = Zend_Registry::get ( 'cnf' );
		$config = Zen_Settings::get('feedback', 'email');
		if (isset($config ['email'])&&($config ['email'] != "")) {
			$emails = explode ( ',', $config ['email'] );
			foreach ( $emails as $rec ) {
				$message = array ('from' => 'feedback@' . $globalConf->domen, 'subject' => 'Новое сообщение на сайте ' . $globalConf->domen, 'text' => $text, 'to' => $rec );
				$email->create ( $message );
			}
			$email->send_messages ();
		}
		return $dparams;
	}
	
	public function setForm($value) {
		$this->_form = $value;
		return $this;
	}
	
	public function getForm() {
		return $this->_form;
	}
	
}